﻿/*
///    This file is part of BannerLib4Net.
///
///    BannerLib4Net is free software: you can redistribute it and/or modify
///    it under the terms of the GNU General Public License as published by
///    the Free Software Foundation, either version 3 of the License, or
///    (at your option) any later version.

///    BannerLib4Net is distributed in the hope that it will be useful,
///    but WITHOUT ANY WARRANTY; without even the implied warranty of
///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///    GNU General Public License for more details.
///
///    You should have received a copy of the GNU General Public License
///    along with BannerLib4Net.  If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using BannerLib4Net.Core.Utilities.Configuration;
using BannerLib4Net.Data.Dao;
using BannerLib4Net.Data.Extensions;
using BannerLib4Net.Data.Models;
using Npgsql;

namespace BannerLib4Net.Postgresql.Dao
{
    public class PostgresqlDao: IDao
    {
        #region IDao Members

        public IDbConnection Connection
        {
            get
            {
                String connStr = SettingsUtility.ReadConfiguredConnectionString();
                NpgsqlConnection cn = new NpgsqlConnection(connStr);
                cn.Open();
                return cn;
            }
        }

        public IList<LookupModel> RetrieveLookupModelsFromView(string viewName)
        {
            IList<LookupModel> retval = new List<LookupModel>();

            NpgsqlCommand cmd = new NpgsqlCommand();
            cmd.CommandText = String.Format("select * from {0}", viewName);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = (NpgsqlConnection)Connection;
            cmd.CommandTimeout = 600;

            NpgsqlDataReader rdr = cmd.ExecuteReader();

            if(!rdr.IsClosed && rdr.HasRows)
            {
                while(rdr.Read())
                {
                    retval.Add(rdr.BindLookupModelBaseProperties(new LookupModel()));
                }
            }

            return retval;

        }

        public IDbCommand CreateProcedureCommand(string procedureName, int timeout = 600)
        {
            NpgsqlCommand cmd = new NpgsqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = procedureName;
            cmd.Connection = (NpgsqlConnection)Connection;
            cmd.CommandTimeout = 600;
            cmd.Transaction = cmd.Connection.BeginTransaction();
            
            return cmd;

        }

        public System.Data.IDataReader ExecuteView(string viewName)
        {

            NpgsqlCommand cmd = new NpgsqlCommand();
            cmd.CommandText = String.Format("select * from {0};", viewName);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = (NpgsqlConnection)Connection;
            cmd.CommandTimeout = 600;

            return cmd.ExecuteReader();
            
        }

        public IDataReader ExecuteCommand(IDbCommand cmd)
        {
            return cmd.ExecuteReader();
        }


        #endregion

        public void ExecuteNonQuery(IDbCommand cmd)
        {
            cmd.ExecuteNonQuery();
        }

        public object ExecuteScalar(string functionName, int timeout = 600)
        {
            NpgsqlCommand cmd = (NpgsqlCommand)this.CreateProcedureCommand(functionName, timeout);
            return cmd.ExecuteScalar();
        }

    }
}
